package my.yuan.ms.rediskafkams.config;

import my.yuan.ms.rediskafkams.controller.vo.MsVO;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.*;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ContainerProperties;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableKafka
public class KafkaConfig {
    @Value("${kafka.brokerAddress}")
    private String brokerAddress;

    //producer配置
    @Bean
    public KafkaTemplate<String, MsVO> kafkaTemplate() {
        KafkaTemplate<String, MsVO> kafkaTemplate = new KafkaTemplate<String, MsVO>(producerFactory());

        //设置总的消息监听器，也可以在发送消息时设置回调
        //kafkaTemplate.setProducerListener();
        return kafkaTemplate;
    }

    @Bean
    public ProducerFactory<String, MsVO> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerAddress);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, MsVoKafkaSerializer.class);
        props.put(ProducerConfig.RETRIES_CONFIG, "1");
        props.put(ProducerConfig.ACKS_CONFIG, "1");
        // See https://kafka.apache.org/documentation/#producerconfigs for more properties

        return props;
    }

    //consumer配置
    @Bean
    KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, MsVO>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, MsVO> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(3);
        //开启批量消费消息
        factory.setBatchListener(true);
        //在监听器提交ack
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
        factory.getContainerProperties().setPollTimeout(100);
        return factory;
    }

    @Bean
    public ConsumerFactory<String, Object> consumerFactory() {
        DefaultKafkaConsumerFactory<String, Object> factory = new DefaultKafkaConsumerFactory<>(consumerConfigs());
        return factory;
    }

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerAddress);
        props.put(ConsumerConfig.CLIENT_ID_CONFIG, "consuer-ms-01");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, MsVoKafkaDeserializer.class);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
        //每次从最新位置拉去，防止重复消费
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");

        return props;
    }


}
